package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import Connection;
	import flash.text.StyleSheet;
	import flash.text.TextFormat;
	import vk.*;
	
	public class Main extends Sprite
	{
		public var common_area:TextField;
		private var input_area:TextField;
		private var u_list_area:TextField;
		private var connection:Connection;
		private var style:StyleSheet;
		private var flashVars: Object = stage.loaderInfo.parameters as Object; 
		private var VK: APIConnection;
		public var userID:int;
		public var first_name:String;
		public var last_name:String;
		public var prevPhrases:Array;
		public var prevPointer:int;
		
		public function Main() {
			
			prevPhrases = [];
			if (!flashVars.api_id) { 
			  // -- For local testing enter you test-code here: 
			  flashVars['api_id'] = 2643265; 
			  flashVars['secret'] = "ca9e532d64";
			  flashVars['sid'] = "3639d039c0d91c2a142f12452fb7a8e07bb002f9dbe4d6a2aaa152105ff8c5";
			  flashVars['test_mode'] = 1;
			  flashVars['viewer_id'] = 9564662; 
			  // -- // 
			}
			userID = flashVars['viewer_id'];
			
			flashVars['test_mode'] = 1;
			VK =  new APIConnection(flashVars);
			createField();
			
			
			VK.api('getProfiles', { uids: flashVars['viewer_id'],fields:'photo' }, authSucceed, authError);
		}
		
		
		public function authSucceed(arr:Object):void {
			this.first_name = arr[0]['first_name'];
			this.last_name =  arr[0]['last_name'];

			this.connection = new Connection(this);
			//addText(arr['photo'] + "</br>");
		}
		public function authError(arr:Object):void {
			addText("Авторизация не пройдена<br/>");
		}
		
		public function addText(text:String):void {
			if (text != null) {
				this.common_area.htmlText = (this.common_area.htmlText + text);
				this.common_area.scrollV = this.common_area.maxScrollV;
			}
		}
		
		public function createField():void {
			style = new StyleSheet;
			
			var p:Object = new Object();
            p.fontStyle = "_sens";
			p.color = "#000000";
			//p.fontFamily = "terminal";
			p.fontSize = 20;
			
			var span:Object = new Object();
			span.fontStyle = "normal";
			span.color = "#000000";
			//span.fontFamily = "terminal";
			span.fontSize = 20;
			
			
			var time:Object = new Object();
            //time.fontWeight = "bold";
			time.color = "#000000";
			time.fontSize = 20;
			//time.fontFamily = "terminal";
			
			var username:Object = new Object();
            username.fontWeight = "bold";
			username.color = "#000000";
			username.fontSize = 20;
			//username.fontFamily = "terminal";
			
			var botname:Object = new Object();
            botname.fontWeight = "bold";
			botname.color = "#FF0000";
			botname.fontSize = 20;
			//botname.fontFamily = "terminal";
			
			var hint:Object = new Object();
            hint.fontWeight = "bold";
			hint.color = "#0000FF";
			hint.fontSize = 20;
			//hint.fontFamily = "terminal";
			
			var answer:Object = new Object();
            answer.fontWeight = "bold";
			answer.color = "#0000FF";
			answer.fontSize = 20;
			//answer.fontFamily = "terminal";
			
			var nobody_won:Object = new Object();
            nobody_won.fontWeight = "bold";
			nobody_won.color = "#FF0000";
			nobody_won.fontSize = 20;
			//nobody_won.fontFamily = "terminal";
			
			var letters_num:Object = new Object();
            letters_num.fontWeight = "bold";
			letters_num.color = "#FF0000";
			letters_num.fontSize = 20;
			//letters_num.fontFamily = "terminal";
			
			
            style.setStyle("p", p);
            style.setStyle("span", span);
            style.setStyle(".time", time);
            style.setStyle(".username", username);
            style.setStyle(".hint", hint);
			style.setStyle(".botname", botname);
			style.setStyle(".answer", answer);
			style.setStyle(".nobody_won", nobody_won);
			style.setStyle(".letters_num", letters_num);

			
			
			common_area = new TextField();
			common_area.width = 807;
			common_area.height = 500;
			common_area.x = 0;
			common_area.y = 0;
			common_area.border = false;
			common_area.wordWrap = true;
			common_area.multiline = true;
			//common_area.background = true;
			//common_area.backgroundColor = 0xefefef;
			
			common_area.styleSheet = style;
			
			var format:TextFormat = new TextFormat;
			format.size = 22;
			
			input_area = new TextField();
			input_area.width = 807;
			input_area.height = 30;
			input_area.x = 0;
			input_area.y = 500;
			input_area.defaultTextFormat = format;
			input_area.type = "input";
			input_area.border = true;
			input_area.borderColor = 0xDDDDDD;
			input_area.background = true;
			input_area.backgroundColor = 0xEFEFEF;
			
			u_list_area = new TextField();
			u_list_area.width = 157;
			u_list_area.height = 530;
			u_list_area.x = 650;
			u_list_area.y = 0;
			u_list_area.border = false;
			u_list_area.background = true;
			u_list_area.backgroundColor = 0xDDDDDD;
			
			input_area.addEventListener(KeyboardEvent.KEY_UP, keyUpListener);
			input_area.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
			
			hideField();
			
			addChild(common_area);
			addChild(input_area);
			//addChild(u_list_area);
		}
		
		public function hideField():void {
			//common_area.visible = false;
			//input_area.visible = false;
			input_area.type = "dynamic";
			//u_list_area.visible = false;
		}
		
		public function showField():void {
			//common_area.visible = true;
			//input_area.visible = true;
			//u_list_area.visible = true;
			input_area.type = "input";
		}
		
		private function keyDownListener(event:KeyboardEvent):void {
			if ( (event.keyCode == 8 || event.keyCode == 46) && event.shiftKey ) {
				input_area.text = "";
			}
			//up
			if (event.keyCode == 38) {
				event.preventDefault();
				if(prevPhrases.length && prevPointer >= 0) {
					input_area.text = prevPhrases[prevPointer];
					prevPointer -= 1;
					if (prevPointer < 0)
						prevPointer = 0;
				}
			}
			//down
			if (event.keyCode == 40) {
				event.preventDefault();
				if(prevPhrases.length && prevPointer < prevPhrases.length-1) {
					input_area.text = prevPhrases[prevPointer];
					prevPointer += 1;
				}
				else if (prevPointer == prevPhrases.length-1) {
					input_area.text = "";
				}
			}
		}
		
		private function keyUpListener(event:KeyboardEvent):void {
			
			//trace(event);

			
			if (event.keyCode == 13) {
				event.preventDefault();
				if (input_area.text != "")
					this.connection.sendCommand(input_area.text);
					prevPhrases.push(input_area.text);
					if (prevPhrases.length > 5) 
						prevPhrases.shift();
					prevPointer = prevPhrases.length - 1;
				input_area.text = "";
			}
		}
	}
	
}
